/*
 * Service.java
 * 
 * This file is part of Netuno Proteu.
 * 
 * CajuScript is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3, or (at your option) 
 * any later version.
 * 
 * CajuScript is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with CajuScript.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.netuno.proteu;

import org.apache.log4j.Logger;

/**
 * Service, receive client and start communication between client and server.
 * @author eduveks
 */
public class Service {
    static Logger logger = Logger.getLogger(Service.class);

    /**
     * Run service.
     */
    public static void run(org.netuno.ServiceData serviceData) {
        Http http = null;
        try {
            http = new Http(serviceData.getInputStream());
            http.setValue("Server", "Netuno");
            http.setValue("Client", serviceData.getClientSocket().getInetAddress().toString());
            http.setValue("PublicDir", Config.getPublic());
            http.setValue("BuildDir", Config.getBuild());
            http.loadForms();
            Proteu proteu = new Proteu(http.getClientHttp(), http.getRequestHead(), http.getRequestPost(), http.getRequestGet(), http.getRequestCookie(), http.getResponseHead(), http.getResponseCookie(), serviceData.getOutputStream());
            proteu.getConfig().set("thread_id", ""+ serviceData.getThreadID());
            Url.build(proteu);
            serviceData.getOutputStream().flush();
        } catch (Throwable t) {
            logger.warn("Client HTTP.", t);
            throw new Error(t);
        } finally {
            Config.setStarting(false);
            http = null;
        }
    }
}
